package com.example.sunmiv1.activity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.adapter.InventoryListAdapter;
import com.example.sunmiv1.base.BaseActivity;
import com.example.sunmiv1.entity.OldInventoryInfo;
import com.example.sunmiv1.entity.ReportLossGoodBean;
import com.example.sunmiv1.utils.LogUtil;
import com.example.sunmiv1.utils.Tools;
import com.example.sunmiv1.utils.TopSmoothScroller;
import com.example.sunmiv1.utils.net.ACache;
import com.example.sunmiv1.utils.net.Common;
import com.example.sunmiv1.utils.net.GsonUtils;
import com.example.sunmiv1.utils.net.HttpUtils;
import com.example.sunmiv1.utils.net.LoadingDialog;
import com.example.sunmiv1.utils.net.TextCallBack;
import com.example.sunmiv1.utils.zxing.Constant;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 盤點頁面.新增
 */

public class NewInventoryActivity extends BaseActivity {

    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.title_title)
    TextView titleTitle;
    @BindView(R.id.inventory_chooseFood_btn)
    LinearLayout inventoryChooseFoodBtn;
    @BindView(R.id.inventory_scanFood_btn)
    LinearLayout inventoryScanFoodBtn;
    @BindView(R.id.inventorySearch_edit)
    EditText inventorySearchEdit;
    @BindView(R.id.inventorySearch_code_new)
    ImageView inventorySearchCodeNew;
    @BindView(R.id.inventorySearch_search_new)
    ImageView inventorySearchSearchNew;
    @BindView(R.id.inventory_new_checkAll)
    TextView inventoryNewCheckAll;
    @BindView(R.id.inventory_new_submit)
    TextView inventoryNewSubmit;
    @BindView(R.id.inventory_new_complete)
    TextView inventoryNewComplete;
    @BindView(R.id.inventory_new_delete)
    TextView inventoryNewDelete;
    @BindView(R.id.inventory_new_operation)
    LinearLayout inventoryNewOperation;
    @BindView(R.id.inventory_new_save)
    TextView inventoryNewSave;
    @BindView(R.id.btn_to_top)
    ImageView btnToTop;
    @BindView(R.id.view_top)
    LinearLayout viewTop;
    @BindView(R.id.ll_save_and_submit)
    LinearLayout llSaveAndSubmit;
    @BindView(R.id.view_footer)
    LinearLayout viewFooter;
    @BindView(R.id.inventory_new_listView)
    RecyclerView inventoryNewListView;

    private ACache aCache;
    private LoadingDialog loadingDialog;
    private InventoryListAdapter inventoryListAdapter;
    private ArrayList<String> notInIdArr;// 已在此頁的貨物id  添加和選擇2個頁面都要維護這個列表
    private OldInventoryInfo oldInventoryInfo;
    private List<ReportLossGoodBean.DataBean.ListBean> choiceList;
    public static boolean opera = false;
    private Intent intent;
    private int status = 1;
    private String order_id = "";

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    setInventoryListAdapter();
                    break;
            }
        }
    };

    @OnClick({R.id.title_back, R.id.inventory_chooseFood_btn, R.id.inventory_scanFood_btn, R.id.inventorySearch_code_new,
            R.id.inventorySearch_search_new, R.id.inventory_new_checkAll, R.id.btn_to_top, R.id.inventory_new_submit,
            R.id.inventory_new_save, R.id.inventory_new_complete, R.id.inventory_new_delete})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                if (searchTimer != null && searchTask != null) {
                    searchTimer.cancel();
                    searchTimer.purge();
                    searchTimer = null;
                    searchTask.cancel();
                }
                finish();
                break;
            case R.id.inventory_chooseFood_btn://選擇商品
                intent = new Intent(this, InventorySearchActivity.class);
                intent.putStringArrayListExtra("not_in_ids", notInIdArr);
                startActivityForResult(intent, 100);
                break;
            case R.id.inventory_scanFood_btn:
                if (searchTimer != null && searchTask != null) {
                    searchTimer.cancel();
                    searchTimer.purge();
                    searchTimer = null;
                    searchTask.cancel();
                }
                Log.e("TAG", "onViewClicked: orderid=============" + order_id);
                if (order_id == null) {
                    order_id = "";
                }
                aCache.put("order_id", order_id);
                scanGood(2, choiceList);
                break;
            case R.id.inventorySearch_code_new://掃碼
                startQrCode();
                break;
            case R.id.inventorySearch_search_new://搜索商品
                if (TextUtils.isEmpty(inventorySearchEdit.getText().toString().trim())) {
                    Toast.makeText(this, "請輸入貨號/條碼/名稱", Toast.LENGTH_SHORT).show();
                    return;
                }
                boolean flag = false;
                String info = inventorySearchEdit.getText().toString().trim();
                for (int i = 0; i < choiceList.size(); i++) {
                    if (info.equals(choiceList.get(i).getGoods_code())) {
                        flag = true;
                        TopSmoothScroller top = new TopSmoothScroller(context);
                        top.setTargetPosition(i);
                        (inventoryNewListView.getLayoutManager()).startSmoothScroll(top);
                        inventoryListAdapter.setSelect(i);
                        break;
                    }
                    if (info.equals(choiceList.get(i).getSku_code())) {
                        flag = true;
                        TopSmoothScroller top = new TopSmoothScroller(context);
                        top.setTargetPosition(i);
                        (inventoryNewListView.getLayoutManager()).startSmoothScroll(top);
                        inventoryListAdapter.setSelect(i);
                        break;
                    }
                    if (info.equals(choiceList.get(i).getGoods_number())) {
                        flag = true;
                        TopSmoothScroller top = new TopSmoothScroller(context);
                        top.setTargetPosition(i);
                        (inventoryNewListView.getLayoutManager()).startSmoothScroll(top);
                        inventoryListAdapter.setSelect(i);
                        break;
                    }
                    if (info.equals(choiceList.get(i).getSku_goods_number())) {
                        flag = true;
                        TopSmoothScroller top = new TopSmoothScroller(context);
                        top.setTargetPosition(i);
                        (inventoryNewListView.getLayoutManager()).startSmoothScroll(top);
                        inventoryListAdapter.setSelect(i);
                        break;
                    }
                    if (info.equals(choiceList.get(i).getGoods_name())) {
                        flag = true;
                        TopSmoothScroller top = new TopSmoothScroller(context);
                        top.setTargetPosition(i);
                        (inventoryNewListView.getLayoutManager()).startSmoothScroll(top);
                        inventoryListAdapter.setSelect(i);
                        break;
                    }
                }
                if (!flag) {
                    Toast.makeText(this, "已選列表中暫無該商品", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.inventory_new_checkAll://操作
                opera = true;
                if (llSaveAndSubmit.getVisibility() == View.GONE) {
                    if ("全選".equals(inventoryNewCheckAll.getText().toString().trim())) {
                        for (ReportLossGoodBean.DataBean.ListBean dataBean : choiceList) {
                            dataBean.setChecked(true);
                        }
                        setInventoryListAdapter();
                        inventoryNewCheckAll.setText(R.string.purRefundAdding_Choose_Item_Check_None);
                    } else {
                        for (ReportLossGoodBean.DataBean.ListBean dataBean : choiceList) {
                            dataBean.setChecked(false);
                        }
                        setInventoryListAdapter();
                        inventoryNewCheckAll.setText(R.string.purRefundAdding_check_all);
                    }
                } else {
                    llSaveAndSubmit.setVisibility(View.GONE);
                    inventoryNewOperation.setVisibility(View.VISIBLE);
                    inventoryNewCheckAll.setText(R.string.purRefundAdding_check_all);
                }
                setInventoryListAdapter();
                break;
            case R.id.inventory_new_submit://提交盤點
                submit(1, "確定要提交盤點？");
                break;
            case R.id.inventory_new_save:
                submit(2, "確定要提交盤點至草稿箱？");
                break;
            case R.id.inventory_new_complete://完成
                opera = false;
                llSaveAndSubmit.setVisibility(View.VISIBLE);
                inventoryNewOperation.setVisibility(View.GONE);
                inventoryNewCheckAll.setText(R.string.operation);
                setInventoryListAdapter();
                break;
            case R.id.inventory_new_delete://刪除選中
                List<String> li = new ArrayList<>();
                if (order_id != null && !TextUtils.isEmpty(order_id)) {
                    for (int i = choiceList.size() - 1; i >= 0; i--) {
                        if (choiceList.get(i).isChecked()) {
                            li.add(choiceList.get(i).getId());
                        }
                    }
                    String s = toNotInIds(li);
                    deleteGood(s);
                } else {
                    for (int i = choiceList.size() - 1; i >= 0; i--) {
                        if (choiceList.get(i).isChecked()) {
                            notInIdArr.remove(choiceList.get(i).getId());
                            choiceList.remove(i);
                        }
                    }
                    setInventoryListAdapter();
                }
                break;
            case R.id.btn_to_top:
                TopSmoothScroller top = new TopSmoothScroller(context);
                top.setTargetPosition(0);
                (inventoryNewListView.getLayoutManager()).startSmoothScroll(top);
                break;
        }
    }

    private String toNotInIds(List<String> strings) {
        if (strings.isEmpty()) {
            return "";
        } else {
            StringBuilder notInIds = new StringBuilder();
            if (strings.size() > 0) {
                notInIds.append(strings.get(0));
            }
            if (strings.size() > 1) {
                for (int i = 1; i < strings.size(); i++) {
                    notInIds.append("," + strings.get(i));
                }
            }
            return notInIds.toString();
        }
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_new_inventory);
        ButterKnife.bind(this);
        titleTitle.setText(R.string.newInventoryTitle);
        aCache = ACache.get(this);
        loadingDialog = new LoadingDialog(this);
    }

    @Override
    protected void initData() {
        choiceList = new ArrayList<>();
        order_id = getIntent().getStringExtra("order_id");
        notInIdArr = new ArrayList<>();
        if (!TextUtils.isEmpty(order_id)) {
            getOldInventory();
        }
    }

    private Timer searchTimer;
    private SearchTimerTask searchTask;


    private class SearchTimerTask extends TimerTask {
        @Override
        public void run() {
            Log.e("TAG", "run: SearchTimerTask===============");
            runOnUiThread(() -> {
                if (choiceList.size() > 0) {
                    status = 3;
                    submitInventory();
                }
            });
        }
    }

    private void deleteGood(String id) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("order_id", aCache.getAsString("order_id"));
            jsonObject.put("inventory_goods_id", id);
            jsonObject = Tools.createParameterCode(jsonObject);
            HttpUtils.post(this, Common.DELETE_GOODS_RECORD, jsonObject, new TextCallBack() {
                @Override
                protected void onSuccess(String text) {
                    try {
                        JSONObject json = new JSONObject(text);
                        if (json.getInt("status") == 1) {
                            for (int i = choiceList.size() - 1; i >= 0; i--) {
                                if (choiceList.get(i).isChecked()) {
                                    notInIdArr.remove(choiceList.get(i).getId());
                                    choiceList.remove(i);
                                }
                            }
                            setInventoryListAdapter();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                protected void onFailure(ResponseException e) {

                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (searchTimer != null && searchTask != null) {
            searchTimer.cancel();
            searchTimer.purge();
            searchTimer = null;
            searchTask.cancel();
        }
        if (searchTimer == null) {
            searchTimer = new Timer();
        }
        searchTask = new SearchTimerTask();
        searchTimer.schedule(searchTask, 20000, 20000);
    }

    @Override
    protected void initListener() {

    }

    private void setInventoryListAdapter() {
        if (inventoryListAdapter == null) {
            inventoryListAdapter = new InventoryListAdapter(this, choiceList);
            inventoryNewListView.setAdapter(inventoryListAdapter);
        } else {
            inventoryListAdapter.notifyDataSetChanged();
        }
        inventoryListAdapter.setOnTextChangeListener((pos, str) -> {
            choiceList.get(pos).setInventory_num(str);
        });
        inventoryListAdapter.setChoose((index, type) -> {
            if (type == 1) {//系統庫存查看詳情
                intent = new Intent(context, StringCodeOperationActivity.class);
                intent.putExtra("operation", 29);
                intent.putExtra("good", choiceList.get(index));
                startActivityForResult(intent, 200);
            } else if (type == 2) {//添加串碼
                intent = new Intent(context, StringCodeOperationActivity.class);
                intent.putExtra("operation", 2);
                intent.putExtra("good", choiceList.get(index));
                startActivityForResult(intent, 200);
            }
        });
    }

    /*獲取繼續盤點商品數據*/
    private void getOldInventory() {
        loadingDialog.show();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("user_type", aCache.getAsString("userType"));
            jsonObject.put("inventory_order_id", order_id);
            jsonObject = Tools.createParameterCode(jsonObject);
            HttpUtils.post(this, Common.GET_OLD_INVENTORY, jsonObject, new TextCallBack() {
                @Override
                protected void onSuccess(String text) {
                    loadingDialog.dismiss();
                    oldInventoryInfo = GsonUtils.JsonToBean(text, OldInventoryInfo.class);
                    if (oldInventoryInfo.getStatus() == 1) {
                        for (OldInventoryInfo.GoodsListBean goodsListBean : oldInventoryInfo.getGoodsList()) {
                            choiceList.add(new ReportLossGoodBean.DataBean.ListBean(goodsListBean));
                            notInIdArr.add(goodsListBean.getId());
                            mHandler.sendEmptyMessage(1);
                        }
                    } else {
                    }
                }

                @Override
                protected void onFailure(ResponseException e) {
                    Toast.makeText(NewInventoryActivity.this, "網絡出錯", Toast.LENGTH_SHORT).show();
                    loadingDialog.dismiss();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /*提交盤點*/
    private void submitInventory() {
        if (status != 3) {
            loadingDialog.show();
        }
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        try {
            jsonObject.put("user_type", aCache.getAsString("userType"));
            for (int i = 0; i < choiceList.size(); i++) {
                JSONObject json = new JSONObject();
                json.put("id", choiceList.get(i).getId());
                json.put("warehouse_id", choiceList.get(i).getWarehouse_id());
                json.put("goods_id", choiceList.get(i).getGoods_id());
                json.put("goods_name", choiceList.get(i).getGoods_name());
                json.put("goods_number", choiceList.get(i).getGoods_number());
                json.put("goods_code", choiceList.get(i).getGoods_code());
                json.put("attribute_state", choiceList.get(i).getAttribute_state());
                json.put("sku_id", choiceList.get(i).getSku_id());
                json.put("sku_code", choiceList.get(i).getSku_code());
                json.put("sku_name", choiceList.get(i).getSku_name());
                json.put("sku_num", choiceList.get(i).getSku_num());
                json.put("sku_goods_number", choiceList.get(i).getSku_goods_number());
                json.put("goods_num", choiceList.get(i).getGoods_num());
                json.put("more_num", choiceList.get(i).getMore_num());
                json.put("less_num", choiceList.get(i).getLess_num());
                json.put("inventory_num", choiceList.get(i).getInventory_num());
                json.put("cost_price", choiceList.get(i).getCost_price());
                json.put("purchase_order_id", choiceList.get(i).getPurchase_order_id());
                json.put("company", choiceList.get(i).getCompany());
                json.put("inventory_goods_id", choiceList.get(i).getInventory_goods_id());
                json.put("serial_number_state", choiceList.get(i).getSerial_number_state());
                if ("1".equals(choiceList.get(i).getSerial_number_state())) {
                    JSONArray serial_list = new JSONArray();
                    for (ReportLossGoodBean.DataBean.ListBean.SerialBean serialBean : choiceList.get(i).getSerialList()) {
                        JSONObject object = new JSONObject();
                        object.put("main_serial_number", serialBean.getMain_serial_number());
                        object.put("auxiliary_serial_number_one", serialBean.getAuxiliary_serial_number_one());
                        object.put("auxiliary_serial_number_two", serialBean.getAuxiliary_serial_number_two());
                        serial_list.put(object);
                    }
                    json.put("serial_list", serial_list);
                }
                LogUtil.e("");
                jsonArray.put(json);
            }
            jsonObject.put("goodsList", jsonArray);
            jsonObject.put("submit_type", status);//1盤點 2保存草稿箱 3實時盤點 按鈕類型
            jsonObject.put("order_id", order_id);//通過繼續盤點按鈕點擊去的頁面提交必須傳盤點訂單id
            jsonObject = Tools.createParameterCode(jsonObject);
            HttpUtils.post(this, Common.SUBMIT_INVENTORY, jsonObject, new TextCallBack() {
                @Override
                protected void onSuccess(String text) {
                    try {
                        JSONObject json = new JSONObject(text);
                        if (status != 3) {
                            loadingDialog.dismiss();
                            if (json.getInt("status") == 1) {
                                choiceList.clear();
                                setInventoryListAdapter();
                                finish();
                            }
                            Toast.makeText(NewInventoryActivity.this, json.getString("message"), Toast.LENGTH_SHORT).show();
                        } else {
                            order_id = json.getJSONObject("data").getString("order_id");
                            JSONArray arr = json.getJSONObject("data").getJSONArray("record_id_arr");
                            for (int i = 0; i < choiceList.size(); i++) {
                                for (int s = 0; s < arr.length(); s++) {
                                    JSONObject d = arr.getJSONObject(s);
                                    if ("2".equals(d.getString("attribute_state"))) {
                                        if (choiceList.get(i).getGoods_id().equals(d.getString("goods_id")) &&
                                                choiceList.get(i).getSku_id().equals(d.getString("sku_id"))) {
                                            choiceList.get(i).setInventory_goods_id(d.getString("inventory_goods_id"));
                                        }
                                    } else {
                                        if (choiceList.get(i).getGoods_id().equals(d.getString("goods_id"))) {
                                            choiceList.get(i).setInventory_goods_id(d.getString("inventory_goods_id"));
                                        }
                                    }
                                }
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                protected void onFailure(ResponseException e) {
                    if (status != 3) {
                        loadingDialog.dismiss();
                    }
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case Constant.REQ_QR_CODE:
                    Bundle bundle = data.getExtras();
                    inventorySearchEdit.setText(bundle.getString(Constant.INTENT_EXTRA_KEY_QR_SCAN));
                    String info = inventorySearchEdit.getText().toString().trim();
                    boolean flag = false;
                    for (int i = 0; i < choiceList.size(); i++) {
                        if (info.equals(choiceList.get(i).getGoods_code())) {
                            flag = true;
                            TopSmoothScroller top = new TopSmoothScroller(context);
                            top.setTargetPosition(i);
                            (inventoryNewListView.getLayoutManager()).startSmoothScroll(top);
                            inventoryListAdapter.setSelect(i);
                            break;
                        }
                        if (info.equals(choiceList.get(i).getSku_code())) {
                            flag = true;
                            TopSmoothScroller top = new TopSmoothScroller(context);
                            top.setTargetPosition(i);
                            (inventoryNewListView.getLayoutManager()).startSmoothScroll(top);
                            inventoryListAdapter.setSelect(i);
                            break;
                        }
                        if (info.equals(choiceList.get(i).getGoods_number())) {
                            flag = true;
                            TopSmoothScroller top = new TopSmoothScroller(context);
                            top.setTargetPosition(i);
                            (inventoryNewListView.getLayoutManager()).startSmoothScroll(top);
                            inventoryListAdapter.setSelect(i);
                            break;
                        }
                        if (info.equals(choiceList.get(i).getSku_goods_number())) {
                            flag = true;
                            TopSmoothScroller top = new TopSmoothScroller(context);
                            top.setTargetPosition(i);
                            (inventoryNewListView.getLayoutManager()).startSmoothScroll(top);
                            inventoryListAdapter.setSelect(i);
                            break;
                        }
                    }
                    if (!flag) {
                        Toast.makeText(this, "已選列表中暫無該商品", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case 100://選擇商品返回的數據
                    List<ReportLossGoodBean.DataBean.ListBean> backList = (List<ReportLossGoodBean.DataBean.ListBean>) data.getSerializableExtra("choice_list");
                    for (ReportLossGoodBean.DataBean.ListBean dataBean : backList) {
                        dataBean.setChecked(false);
                        notInIdArr.add(dataBean.getId());
                    }
                    choiceList.addAll(backList);
                    setInventoryListAdapter();
                    break;
                case 110:
                    order_id = aCache.getAsString("order_id");
                    List<ReportLossGoodBean.DataBean.ListBean> s = (List<ReportLossGoodBean.DataBean.ListBean>) data.getSerializableExtra("list");
                    for (ReportLossGoodBean.DataBean.ListBean dataBean : s) {
                        dataBean.setChecked(false);
                        notInIdArr.add(dataBean.getId());
                    }
                    choiceList.clear();
                    choiceList.addAll(s);
                    setInventoryListAdapter();
                    break;
                case 200:
                    ReportLossGoodBean.DataBean.ListBean bean = (ReportLossGoodBean.DataBean.ListBean) data.getSerializableExtra("bean");
                    int index = 0;
                    for (int t = 0; t < choiceList.size(); t++) {
                        if (bean.getGoods_id().equals(choiceList.get(t).getGoods_id())) {
                            if ("2".equals(bean.getAttribute_state())) {
                                if (bean.getSku_id().equals(choiceList.get(t).getSku_id())) {
                                    index = t;
                                    break;
                                }
                            } else {
                                index = t;
                                break;
                            }
                        }
                    }
                    choiceList.set(index, bean);
                    setInventoryListAdapter();
                    break;
            }
        }

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        // 必不可少，否則所有的組件都不會有TouchEvent了
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);

    }

    public boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = {0, 0};
            //獲取輸入框當前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 點擊的是輸入框區域，保留點擊EditText的事件
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    /**
     * @param i2 //1.盤點 2.保存草稿箱
     * @param s  提示內容
     */
    private void submit(int i2, String s) {
        if (choiceList.size() == 0) {
            Toast.makeText(this, "請先選擇商品", Toast.LENGTH_SHORT).show();
            return;
        }
        if ("2".equals(aCache.getAsString("supplier_type"))) {
            for (ReportLossGoodBean.DataBean.ListBean bean : choiceList) {
                if (bean.getGoods_num() < Double.parseDouble(TextUtils.isEmpty(bean.getInventory_num()) ? "0.0" : bean.getInventory_num())) {
                    if (TextUtils.isEmpty(bean.getPurchase_order_id()) && bean.getPur_id().size() > 0) {
                        Toast.makeText(this, bean.getGoods_name() + "的當盤點數量大於系統庫存\n需要選擇批次號", Toast.LENGTH_SHORT).show();
                        return;
                    }
                }
            }
        }
        for (int i = 0; i < choiceList.size(); i++) {
            if (TextUtils.isEmpty(choiceList.get(i).getInventory_num()) || Double.parseDouble(choiceList.get(i).getInventory_num()) < 0) {
                Toast.makeText(this, "請先盤點" + choiceList.get(i).getGoods_name() + "的庫存", Toast.LENGTH_SHORT).show();

                TopSmoothScroller top = new TopSmoothScroller(context);
                top.setTargetPosition(i);
                (inventoryNewListView.getLayoutManager()).startSmoothScroll(top);
                inventoryListAdapter.setSelect(i);
                return;
            }
        }
        status = i2;
        new AlertDialog.Builder(this).setTitle("提示")//設置對話框標題
                .setMessage(s)//設置顯示的內容
                .setPositiveButton("確定", (dialog, which) -> {//確定按鈕的響應事件
                    submitInventory();
                }).setNegativeButton("取消", (dialog, which) -> {//取消按鈕的響應事件
        }).show();//顯示此對話框
    }

}
